package com.gm.wholesale.module.kind.ui;

import android.text.InputFilter;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.widget.BaseAdapter;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.kind.api.IKindApi;
import com.gm.wholesale.module.kind.bean.KindBean;
import com.gm.wholesale.module.kind.model.KindModel;
import com.gm.wholesale.view.DeleteFunctionView;

import io.reactivex.Observable;

public class KindListFragment extends BaseGridFragment<KindBean, BaseListResp<KindBean>> {
    @Override
    public void initPullToRefreshListView() {
        super.initPullToRefreshListView();
        setBodybg(getResources().getColor(R.color.page_bg));
        mGridView.setNumColumns(4);
        mGridView.setHorizontalSpacing(10);
        mGridView.setVerticalSpacing(10);
    }
    @Override
    public void onTouchClick() {
       if (deleteFunctionView!=null){
           deleteFunctionView.hidView();
       }
    }
    @Override
    public void onRefreshView() {
        hidDeleteFunctionView();
        super.onRefreshView();

    }

    private DeleteFunctionView deleteFunctionView;

    @Override
    protected BaseAdapter createTempBaseAdapter() {
        return new CommonAdapter<>(getActivity(), mDatas, R.layout.list_kind_item) {
            @Override
            public void convert(ViewHolder viewHolder, final KindBean data) {
                viewHolder.setText(R.id.kindNameTv, data.getName());
                viewHolder.getConvertView().setOnClickListener(v -> {
                    if (deleteFunctionView != null) {
                        deleteFunctionView.hidView();
                        deleteFunctionView = null;
                        return;
                    }
                    showUpdateUnitD(data);
                });

                viewHolder.getConvertView().setOnLongClickListener(v -> {
                    hidDeleteFunctionView();
                    deleteFunctionView = viewHolder.getView(R.id.deleteFunctionView);
                    deleteFunctionView.showView(v1 -> deleteKind(data));
                    return false;
                });
            }
        };
    }

    @Override
    public void onGridScrollStateChanged() {
        hidDeleteFunctionView();
    }

    private void hidDeleteFunctionView(){
        if (deleteFunctionView != null) {
            deleteFunctionView.hidView();
            deleteFunctionView = null;
        }
    }

    private void deleteKind(KindBean kindBean) {
        KindModel.deleteKind((IBaseView) getActivity(), kindBean.getId(), kindBean.getVersion(), new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                ToastUtil.showToast("删除成功");
                updateKind();
                refreshData();
            }
        });
    }

    private void showUpdateUnitD(KindBean kind) {
        InputDialog dialog = DialogUtil.showInputDialog(getActivity(), "编辑分类", kind.getName(), new InputDialog.OnSubMitBtnClickListener() {
            @Override
            public void onSubMitBtnClick(String inputData, InputDialog inputDialog) {
                if (TextUtils.isEmpty(inputData)) {
                    ToastUtil.showToast("分类名称不能为空");
                    return;
                }
                ArrayMap<String, Object> data = new ArrayMap<>();
                data.put("name", inputData);
                data.put("version", kind.getVersion());
                KindModel.updateKind((IBaseView) getActivity(), kind.getId(), data, new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        inputDialog.dismiss();
                        ToastUtil.showToast("修改成功");
                        updateKind();
                        refreshData();
                    }
                });

            }
        });
        dialog.setEdInputFilter(new InputFilter[]{new InputFilter.LengthFilter(8)});
    }

    @Override
    protected HttpServicesHolder<?, BaseListResp<KindBean>> getHttpServicesHolder(long index) {
        return new HttpServicesHolder<IKindApi, BaseListResp<KindBean>>() {
            @Override
            public Observable<BaseListResp<KindBean>> getHttpObservable(IKindApi api) {
                return api.getCategory();
            }
        };
    }

    //更新分类
    public void updateKind() {
        EvenBusUtil.updateGoodKind();
    }
}
